package canglang.monitor.cases.service.impl;

import canglang.monitor.cases.model.*;
import canglang.monitor.cases.service.*;
import canglang.monitor.cases.dao.CaseLightDAO;
import canglang.monitor.cases.to.*;
import canglang.monitor.core.base.pagination.PaginatedListHelper;
import canglang.monitor.core.base.util.SortUtil;
import canglang.monitor.core.base.model.BaseModel;
import canglang.monitor.user.model.UserModel;

import java.util.*;

public class CaseLightMgrImpl implements CaseLightMgr {

    private CaseOneMgr caseOneMgr;

    private CaseTwoMgr caseTwoMgr;

    private CaseThreeMgr caseThreeMgr;

    private CaseFourMgr caseFourMgr;

    private CaseFiveMgr caseFiveMgr;

    private CaseSixMgr caseSixMgr;

    private CaseSevenMgr caseSevenMgr;

    private CaseEightMgr caseEightMgr;

    private CaseNineMgr caseNineMgr;

    private CaseTenMgr caseTenMgr;

    private CaseLightDAO caseLightDAO;


    public void setCaseOneMgr(CaseOneMgr caseOneMgr) {
        this.caseOneMgr = caseOneMgr;
    }

    public void setCaseTwoMgr(CaseTwoMgr caseTwoMgr) {
        this.caseTwoMgr = caseTwoMgr;
    }

    public void setCaseThreeMgr(CaseThreeMgr caseThreeMgr) {
        this.caseThreeMgr = caseThreeMgr;
    }

    public void setCaseFourMgr(CaseFourMgr caseFourMgr) {
        this.caseFourMgr = caseFourMgr;
    }

    public void setCaseFiveMgr(CaseFiveMgr caseFiveMgr) {
        this.caseFiveMgr = caseFiveMgr;
    }

    public void setCaseLightDAO(CaseLightDAO caseLightDAO) {
        this.caseLightDAO = caseLightDAO;
    }

    public void setCaseSixMgr(CaseSixMgr caseSixMgr) {
        this.caseSixMgr = caseSixMgr;
    }

    public void setCaseSevenMgr(CaseSevenMgr caseSevenMgr) {
        this.caseSevenMgr = caseSevenMgr;
    }

    public void setCaseEightMgr(CaseEightMgr caseEightMgr) {
        this.caseEightMgr = caseEightMgr;
    }

    public CaseNineMgr getCaseNineMgr() {
        return caseNineMgr;
    }

    public void setCaseNineMgr(CaseNineMgr caseNineMgr) {
        this.caseNineMgr = caseNineMgr;
    }

    public CaseTenMgr getCaseTenMgr() {
        return caseTenMgr;
    }

    public void setCaseTenMgr(CaseTenMgr caseTenMgr) {
        this.caseTenMgr = caseTenMgr;
    }

    public CaseLightModel addCaseLight(CaseLightModel caseLightModel) {
        int id = caseLightDAO.addCaseLight(caseLightModel);
        return caseLightDAO.getCaseLight(id);
    }

    public CaseLightModel getCaseLight(int recordId) {
        return caseLightDAO.getCaseLight(recordId);
    }

    public CaseLightModel saveCaseLight(CaseLightModel caseLightModel) {
        return caseLightDAO.saveCaseLight(caseLightModel);
    }

    public void deleteCaseLight(int recordId) {
        caseLightDAO.deleteCaseLight(recordId);
    }

    @SuppressWarnings("unchecked")
    public HashMap getAllActiveLights(UserModel user) {
        HashMap allMap = new HashMap();

        List whiteList = caseLightDAO.getWhiteLightList(user);
        List blueList = caseLightDAO.getBlueLightList(user);
        List purpleList = caseLightDAO.getPurpleLightList(user);
        List yellowList = caseLightDAO.getYellowLightList(user);
        List redList = caseLightDAO.getRedLightList(user);

        List wList = new ArrayList();
        List bList = new ArrayList();
        List pList = new ArrayList();
        List yList = new ArrayList();
        List rList = new ArrayList();

        List bAttachList = new ArrayList();
        List pAttachList = new ArrayList();
        List yAttachList = new ArrayList();
        List rAttachList = new ArrayList();

        List tmpList;

        boolean isEscalator;

        boolean isDuplicate;

        CaseLightModel c1;

        CaseLightModel c2;

//===================================blue=======================================================
        if (blueList != null && blueList.size() > 0) {
            for (Object obj : blueList) {
                c1 = (CaseLightModel) obj;
                if ("Y".equalsIgnoreCase(c1.getPplInd())) {
                    if ("Y".equalsIgnoreCase(c1.getProcInd())) {
                        rList.add(c1);
                        rAttachList.add(retrieveAttach(c1));
                    } else {
                        yList.add(c1);
                        yAttachList.add(retrieveAttach(c1));
                    }
                } else {
                    bList.add(c1);
                    bAttachList.add(retrieveAttach(c1));
                }
            }
        }
//===================================purple=======================================================
        tmpList = new ArrayList();
        tmpList.addAll(purpleList);
        for (int i = 0; i < purpleList.size(); i++) {
            c1 = (CaseLightModel) purpleList.get(i);
            isEscalator = false;
            for (int j = 0; j < tmpList.size(); j++) {
                c2 = (CaseLightModel) tmpList.get(j);
                if (c1.getDeptId() == c2.getDeptId()) {
                    if (i != j) {
                        isEscalator = true;
                        break;
                    }
                }
            }
            if (isEscalator) {
                yList.add(c1);
                yAttachList.add(retrieveAttach(c1));
            } else {
                pList.add(c1);
                pAttachList.add(retrieveAttach(c1));
            }
        }

//===================================yellow=======================================================
        for (Object obj : yellowList) {
            c1 = (CaseLightModel) obj;
            yAttachList.add(retrieveAttach(c1));
        }
        yList.addAll(yellowList);

//===================================red=======================================================                
        for (Object obj : redList) {
            c1 = (CaseLightModel) obj;
            rAttachList.add(retrieveAttach(c1));
        }
        rList.addAll(redList);

//===================================remove duplicate=======================================================
        tmpList = new ArrayList();
        if (whiteList != null && whiteList.size() > 0) {
            for (Object obj : whiteList) {
                isDuplicate=false;
                c1 = (CaseLightModel) obj;
                for (Object tmpObj : tmpList) {
                    c2 = (CaseLightModel) tmpObj;
                    if (c1.getDeptId() == c2.getDeptId()) {
                        isDuplicate=true;
                        break;
                    }
                }
                if(!isDuplicate)
                tmpList.add(c1);
            }
        }
        wList.addAll(tmpList);

        tmpList = new ArrayList();
        if (bList.size() > 0) {
            for (Object obj : bList) {
                isDuplicate=false;
                c1 = (CaseLightModel) obj;
                for (Object tmpObj : tmpList) {
                    c2 = (CaseLightModel) tmpObj;
                    if (c1.getDeptId() == c2.getDeptId()) {
                        isDuplicate=true;
                        break;
                    }
                }
                if(!isDuplicate)
                tmpList.add(c1);
            }
        }
        bList=new ArrayList();
        bList.addAll(tmpList);

        tmpList = new ArrayList();
        if (pList.size() > 0) {
            for (Object obj : pList) {
                isDuplicate=false;
                c1 = (CaseLightModel) obj;
                for (Object tmpObj : tmpList) {
                    c2 = (CaseLightModel) tmpObj;
                    if (c1.getDeptId() == c2.getDeptId()) {
                        isDuplicate=true;
                        break;
                    }
                }
                if(!isDuplicate)
                tmpList.add(c1);
            }
        }
        pList=new ArrayList();
        pList.addAll(tmpList);

        tmpList = new ArrayList();
        if (yList.size() > 0) {
            for (Object obj : yList) {
                isDuplicate=false;
                c1 = (CaseLightModel) obj;
                for (Object tmpObj : tmpList) {
                    c2 = (CaseLightModel) tmpObj;
                    if (c1.getDeptId() == c2.getDeptId()) {
                        isDuplicate=true;
                        break;
                    }
                }
                if(!isDuplicate)
                tmpList.add(c1);
            }
        }
        yList=new ArrayList();
        yList.addAll(tmpList);

        tmpList = new ArrayList();
        if (rList.size() > 0) {
            for (Object obj : rList) {
                c1 = (CaseLightModel) obj;
                isDuplicate=false;
                for (Object tmpObj : tmpList) {
                    c2 = (CaseLightModel) tmpObj;
                    if (c1.getDeptId() == c2.getDeptId()) {
                        isDuplicate=true;
                        break;
                    }
                }
                if(!isDuplicate)
                tmpList.add(c1);
            }
        }
        rList=new ArrayList();
        rList.addAll(tmpList);


        allMap.put("white", wList);
        allMap.put("blue", bList);
        allMap.put("purple", pList);
        allMap.put("yellow", yList);
        allMap.put("red", rList);
        allMap.put("rAttachList", rAttachList);
        allMap.put("yAttachList", yAttachList);
        allMap.put("pAttachList", pAttachList);
        allMap.put("bAttachList", bAttachList);
        return allMap;
    }


    public void updateCaseFivePplLight(int caseId, String pplInd, String updateId) {
        caseLightDAO.updateCaseFivePplLight(caseId, pplInd, updateId);
    }

    public void updateCaseFiveProcLight(int caseId, String procInd, String updateId) {
        caseLightDAO.updateCaseFiveProcLight(caseId, procInd, updateId);
    }

    public List searchClientSummary() {
        List list1 = caseSixMgr.getCaseSixList(new SearchSixTO());
        List list2 = caseSevenMgr.getCaseSevenList(new SearchSevenTO());
        List list3 = caseEightMgr.getCaseEightList(new SearchEightTO());
        List list4 = caseNineMgr.getCaseNineList(new SearchNineTO());
        List list5 = caseTenMgr.getCaseTenList(new SearchTenTO());
        List list = new ArrayList();
        list.addAll(list1);
        list.addAll(list2);
        list.addAll(list3);
        list.addAll(list4);
        list.addAll(list5);
        SortUtil.sort(list, "createDt", false);
        if (list.size() > 10) list = list.subList(0, 10);
        return list;
    }

    public PaginatedListHelper searchMoreSummary(PaginatedListHelper helper) {
        List list1 = caseSixMgr.getCaseSixList(new SearchSixTO());
        List list2 = caseSevenMgr.getCaseSevenList(new SearchSevenTO());
        List list3 = caseEightMgr.getCaseEightList(new SearchEightTO());
        List list4 = caseNineMgr.getCaseNineList(new SearchNineTO());
        List list5 = caseTenMgr.getCaseTenList(new SearchTenTO());
        List list = new ArrayList();
        list.addAll(list1);
        list.addAll(list2);
        list.addAll(list3);
        list.addAll(list4);
        list.addAll(list5);
        SortUtil.sort(list, helper.getSortCriterion(), helper.getSortDirection() == "asc");
        helper.setFullListSize(list.size());
        if (list.size() >= helper.getToIndex()) {
            list = list.subList(helper.getFromIndex() - 1, helper.getToIndex());
        } else {
            if (list.size() >= helper.getFromIndex()) {
                list = list.subList(helper.getFromIndex() - 1, list.size());
            }
        }
        helper.setList(list);
        return helper;
    }

    public void removeLight(String lightType, int deptId) {
        HashMap map = this.getAllActiveLights(null);
        List list = new ArrayList();
        CaseLightModel c;
        if (lightType.equalsIgnoreCase("white")) {
            list = (ArrayList) map.get("white");
        } else if (lightType.equalsIgnoreCase("blue")) {
            list = (ArrayList) map.get("blue");
        } else if (lightType.equalsIgnoreCase("purple")) {
            list = (ArrayList) map.get("purple");
        } else if (lightType.equalsIgnoreCase("yellow")) {
            list = (ArrayList) map.get("yellow");
        } else if (lightType.equalsIgnoreCase("red")) {
            list = (ArrayList) map.get("red");
        }
        for (int i = 0; i < list.size(); i++) {
            c = (CaseLightModel) list.get(i);
            if (c.getDeptId() == deptId) {
                caseLightDAO.removeLight(c);
            }
        }
    }

    public void updateLightDept(String caseType, int caseId, int deptId) {
        caseLightDAO.updateLightDept(caseType, caseId, deptId);
    }

    public void manualAdd(CaseLightModel caseLightModel) {
        caseLightDAO.addCaseLight(caseLightModel);
    }

    private Object retrieveAttach(CaseLightModel caseLightModel) {
        if (caseLightModel.getCaseType().equalsIgnoreCase("01")) {
            if(caseLightModel.getCaseId()!=0){
                CaseOneModel c = caseOneMgr.getCaseOne(caseLightModel.getCaseId());
                PublishAttachModel m = new PublishAttachModel();
                m.setCaseId(c.getCaseOneId());
                m.setCaseType(caseLightModel.getCaseType());
                m.setFileName(c.getUpload1FileName());
                return m;
            }
        } else if (caseLightModel.getCaseType().equalsIgnoreCase("02")) {
            if(caseLightModel.getCaseId()!=0){
                CaseTwoModel c = caseTwoMgr.getCaseTwo(caseLightModel.getCaseId());
                PublishAttachModel m = new PublishAttachModel();
                m.setCaseId(c.getCaseTwoId());
                m.setCaseType(caseLightModel.getCaseType());
                m.setFileName(c.getUpload1FileName());
                return m;
            }
        } else if (caseLightModel.getCaseType().equalsIgnoreCase("03")) {
             if(caseLightModel.getCaseId()!=0){
                CaseThreeModel c = caseThreeMgr.getCaseThree(caseLightModel.getCaseId());
                PublishAttachModel m = new PublishAttachModel();
                m.setCaseId(c.getCaseThreeId());
                m.setCaseType(caseLightModel.getCaseType());
                m.setFileName(c.getUpload1FileName());
                return m;
             }

        } else if (caseLightModel.getCaseType().equalsIgnoreCase("04")) {
            if(caseLightModel.getCaseId()!=0){
                CaseFourModel c = caseFourMgr.getCaseFour(caseLightModel.getCaseId());
                PublishAttachModel m = new PublishAttachModel();
                m.setCaseId(c.getCaseFourId());
                m.setCaseType(caseLightModel.getCaseType());
                m.setFileName(c.getUpload1FileName());
                return m;
            }

        } else if (caseLightModel.getCaseType().equalsIgnoreCase("05")) {
            if(caseLightModel.getCaseId()!=0){
                CaseFiveModel c = caseFiveMgr.getCaseFive(caseLightModel.getCaseId());
                PublishAttachModel m = new PublishAttachModel();
                m.setCaseId(c.getCaseFiveId());
                m.setCaseType(caseLightModel.getCaseType());
                m.setFileName(c.getUpload1FileName());
                return m;
            }
        }
        return null;
    }

    public void deleteUnuseLight(String caseType, int caseId) {
        caseLightDAO.deleteUnuseLight(caseType, caseId);
    }

}


